package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.ArchiveDefectReq;
import com.ruibang.glass.quality.domain.req.EveryHourArchiveDefectReq;
import com.ruibang.glass.quality.domain.resp.ReinvestmentInfo;
import com.ruibang.glass.quality.entity.EveryHourArchiveDefect;
import com.ruibang.glass.quality.mapper.EveryHourArchiveDefectMapper;
import com.ruibang.glass.quality.service.EveryHourArchiveDefectService;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 每小时归档缺陷统计信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2023-12-13
 */
@Service
public class EveryHourArchiveDefectServiceImpl extends ServiceImpl<EveryHourArchiveDefectMapper, EveryHourArchiveDefect> implements EveryHourArchiveDefectService {

    @Override
    public List<ReinvestmentInfo> getHourReinvestmentNum(ArchiveDefectReq archiveDefectReq){
        return baseMapper.getHourReinvestmentNum(archiveDefectReq);
    }

    @Override
    public void deleteByParam(ArchiveDefectReq archiveDefectReq) {
        baseMapper.deleteByParam(archiveDefectReq);
    }

    @Override
    public PageResult<EveryHourArchiveDefect> queryPage(PageRequest<EveryHourArchiveDefectReq> pageRequest) {
        final LambdaQueryWrapper<EveryHourArchiveDefect> lambda = new QueryWrapper<EveryHourArchiveDefect>().lambda();
        this.buildCondition(lambda,pageRequest.getParam());
        final IPage<EveryHourArchiveDefect> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        PageResult<EveryHourArchiveDefect> pr = new PageResult(page);
        return pr;
    }


    @Override
    public List<EveryHourArchiveDefect> queryEveryHourArchiveDefects(EveryHourArchiveDefectReq defectReq) {
        final LambdaQueryWrapper<EveryHourArchiveDefect> lambda = new QueryWrapper<EveryHourArchiveDefect>().lambda();
        this.buildCondition(lambda,defectReq);
        return this.list(lambda);
    }

    @Override
    public List<EveryHourArchiveDefect> queryEveryHourByTime(ArchiveDefectReq archiveDefectReq) {
        return baseMapper.queryEveryHourByTime(archiveDefectReq);
    }


    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<EveryHourArchiveDefect> lambda, EveryHourArchiveDefectReq param){
        if(null == param) return;

        if(param.getStartTime()!=null && param.getEndTime()!=null){
            lambda.ge(EveryHourArchiveDefect::getDefectTime, param.getStartTime());
            lambda.le(EveryHourArchiveDefect::getDefectTime, param.getEndTime());
        }
        if(!StringUtils.isEmpty(param.getCheckMachineCode())){
            lambda.eq(EveryHourArchiveDefect::getCheckMachineCode, param.getCheckMachineCode());
        }

        lambda.orderBy(true,false, EveryHourArchiveDefect::getDefectTime);
    }
}
